package com.example.english.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.english.App;
import com.example.english.R;
import com.example.english.Utils;
import com.example.english.db.UserBean;

import java.util.List;

public class LoginActivity extends AppCompatActivity {

    private EditText et_account,et_pwd;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Utils.setBarColor(this,true);
        et_account = findViewById(R.id.et_account);
        et_pwd = findViewById(R.id.et_pwd);
    }

    public void onLogin(View view){
        if(TextUtils.isEmpty(et_account.getText().toString()) || TextUtils.isEmpty(et_pwd.getText().toString())){
            Toast.makeText(this ,"请输入账号密码",Toast.LENGTH_LONG).show();
            return;
        }
        List<UserBean> userBeanList = App.dbsqLite.getList();
        if(userBeanList.size() <= 0){
            Toast.makeText(this ,"请先注册账号",Toast.LENGTH_LONG).show();
            return;
        }
        boolean isAccount = false;
        for (int i =0 ; i < userBeanList.size() ;i++){
            if(userBeanList.get(i).getAccount().equals(et_account.getText().toString())
                    && userBeanList.get(i).getPassword().equals(et_pwd.getText().toString())){
                isAccount = true;
                break;
            }
        }

        if(!isAccount){
            Toast.makeText(this ,"账号密码错误或账号不存在",Toast.LENGTH_LONG).show();
            return;
        }

        App.account = et_account.getText().toString();

        Intent i = new Intent(LoginActivity.this,MainActivity.class);
        startActivity(i);
        finish();
    }

    public void onRegister(View view){
        Intent it = new Intent(this ,RegisterActivity.class);
        startActivity(it);
    }

}
